using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using HappySprite;
using HappySprite.Simulation;

namespace HappyInvaders
{
    class ShelterEntity : Entity
    {
        ShelterBitmap bitmap = new ShelterBitmap();

        public ShelterEntity()
        {
            Item.CollisionShape = Box.CreateFromSize(Constants.ShelterWidth, Constants.ShelterHeight);
            Item.CollisionGroup = Constants.ShelterGroup;
            Item.CollisionMask = Constants.BulletGroup | Constants.BombGroup | Constants.InvaderGroup;
 
            Item.Collision += CollisionHandler;
        }

        internal override void OnAdd()
        {
            bitmap.Reset();
        }

        protected override bool ShouldUpdate
        {
            get { return !PositionCurrent; }
        }

        public bool CheckBullet(Vector2 position)
        {
            // Get X and Y adjusted for top of bullet
            float x = position.X;
            float y = position.Y + Constants.BulletHeight * 0.5f;

            int row;
            int col; 
            ConvertPositionToRowColumn(x, y, out col, out row);

            return bitmap.CheckBullet(col, row);
        }

        public bool CheckBomb(Vector2 position)
        {
            // Get X and Y adjusted for bottom of bomb
            float x = position.X;
            float y = position.Y - Constants.BombHeight * 0.5f;

            int row;
            int col;
            ConvertPositionToRowColumn(x, y, out col, out row);

            return bitmap.CheckBomb(col, row);
        }

        public override void Update()
        {
            base.Update();

            if (bitmap.HasChanged)
            {
                OnTextureChanged(bitmap.GetBitArray());
                bitmap.HasChanged = false;
            }
        }

        void ConvertPositionToRowColumn(float x, float y, out int col, out int row)
        {
            // Convert into row and column
            Vector2 v = Item.Transform.Position;
            col = (int)(24f * (x - v.X + 0.5f * Constants.ShelterWidth) / Constants.ShelterWidth);
            row = (int)(16f * (y - v.Y + 0.5f * Constants.ShelterHeight) / Constants.ShelterHeight);

            // Sanity check (You see a formless spawn...lose 2d6 SAN)
            if (col < 0) col = 0;
            if (col > 23) col = 23;
            if (row < 0) row = 0;
            if (row > 15) row = 15;
        }

        void CollisionHandler(CollisionEventArgs e)
        {
            InvaderEntity invader = e.Other.Tag as InvaderEntity;

            if (invader == null)
                return;

            BoundingRectangle rectangle = e.Other.CollisionShape.GetBounds(e.Other.Transform);

            int startRow;
            int startCol;
            int endRow;
            int endCol;

            ConvertPositionToRowColumn(rectangle.Min.X, rectangle.Min.Y, out startCol, out startRow);
            ConvertPositionToRowColumn(rectangle.Max.X, rectangle.Max.Y, out endCol, out endRow);

            bitmap.ClearRange(startCol, endCol, startRow, endRow);
        }
    }
}
